﻿//definir Separadores Decimales
Ext.onReady(function () {
    var F = Ext.util.Format;
    F.thousandSeparator = ',';
    F.decimalSeparator = '.';
});

// IE Evitar seleccion de texto
document.onselectstart = function () {
    if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password")
        return false
    else
        return true;
};

// FIREFOX Evitar seleccion de texto
if (window.sidebar) {
    document.onmousedown = function (e) {
        var obj = e.target;
        if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD")
            return true;
            /*else if (obj.tagName==”BUTTON”){
                return true;
            }*/
        else
            return false;
    }
};

var lookup = {};

var makeTab = function (id, url, title) {
    var tab,
        hostName,
        node,
        tabTip;

    if (id === "-") {
        id = Ext.id(undefined, "extnet");
        lookup[url] = id;
    }

    tabTip = url.replace(/^\//g, "");
    tabTip = tabTip.replace(/\/$/g, "");
    tabTip = tabTip.replace(/\//g, " > ");
    tabTip = tabTip.replace(/_/g, " ");

    hostName = window.location.protocol + "//" + window.location.host;

    tab = App.modulosTabs.add(new Ext.panel.Panel({
        id: id,
        title: title,
        tabTip: tabTip,
        hideMode: "offsets",

        loader: {
            renderer: "frame",
            url: hostName + url,
            listeners: {
                beforeload: function () {
                    this.target.body.mask('Cargando modulo...');
                },
                load: function (loader) {
                    this.target.body.unmask();
                }
            }
        },
        closable: true
    }));

    setTimeout(function () {
        App.modulosTabs.setActiveTab(tab);
    }, 250);

    var node = App.modulosTree.getStore().getNodeById(id),
        expandAndSelect = function (node) {
            var view = App.modulosTree.getView(),
                originalAnimate = view.animate;

            view.animate = false;
            node.bubble(function (node) {
                node.expand(false);
            });

            App.modulosTree.getSelectionModel().select(node);
            view.animate = originalAnimate;
        };

    if (node) {
        expandAndSelect(node);
    } else {
        App.modulosTree.on("load", function (node) {
            node = App.modulosTree.getStore().getNodeById(id);
            if (node) {
                expandAndSelect(node);
            }
        }, this, { delay: 10, single: true });
    }
};

var loadModule = function (href, id, title) {
    var tab = App.modulosTabs.getComponent(id),
        lObj = lookup[href];

    if (id == "-") {
        App.direct.GetHashCode(href, {
            success: function (result) {
                loadModule(href, "e" + result, title);
            }
        });

        return;
    }

    lookup[href] = id;

    if (tab) {
        App.modulosTabs.setActiveTab(tab);
    } else {
        if (Ext.isEmpty(title)) {
            var m = /(\w+)\/$/g.exec(href);
            title = m == null ? "[No name]" : m[1];
        }

        title = title.replace(/<span>&nbsp;<\/span>/g, "");
        title = title.replace(/_/g, " ");
        makeTab(id, href, title);
    }
};

var onTreeItemClick = function (record, e) {
    if (record.isLeaf()) {
        e.stopEvent();
        loadModule(record.get('href'), record.getId(), record.get('text'));
    } else {
        record[record.isExpanded() ? 'collapse' : 'expand']();
    }
};